package com.wxy.school.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wxy.school.common.R;
import com.wxy.school.dto.AddressBookDto;
import com.wxy.school.entity.AddressBook;
import com.wxy.school.service.AddressBookService;
import com.wxy.school.utils.BaseContext;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 王祥宇
 * @projectName school
 * @packageName com.wxy.school.controller
 * @data 2023/3/29 17:29
 * @description: 地址簿Controller管理层
 * @version: 1.0
 */
@RestController
@RequestMapping("school/addressBook")
public class AddressBookController {

    @Resource
    private AddressBookService addressBookService;
    
    /**
     * @description: 添加地址功能
     * @author: 王祥宇 
     * @param: addressBookDto 
     * @return: com.wxy.school.common.R<java.lang.String>
     */
    @PostMapping("/addAddress")
    public R<String> add(@RequestBody AddressBookDto addressBookDto){
        // 将登陆用户存入线程
        BaseContext.setThreadLocal(addressBookDto.getUsername());
        // 判断用户新增的地址是否为默认地址  如果不是则直接存储
        if (addressBookDto.getIsDefault() == 1){
            // 如果用户新增的地址是默认地址 则查询该用户是否已有其他的默认地址 如果没有直接存储
            LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(AddressBook::getIsDefault, addressBookDto.getIsDefault());
            AddressBook addressBook = addressBookService.getOne(lambdaQueryWrapper);
            if (addressBook != null){
                // 如果用户已有其他的默认地址 则将原默认地址取消并存储新的地址为默认地址
                addressBook.setIsDefault(0);
                addressBookService.updateById(addressBook);
            }
        }
        AddressBook addressBook = new AddressBook();
        BeanUtils.copyProperties(addressBookDto, addressBook, "username");
        addressBookService.save(addressBook);
        return R.success("添加成功");
    }
    
    /**
     * @description: 根据用户id查询用户的所有地址信息
     * @author: 王祥宇 
     * @param: id 用户的id
     * @return: com.wxy.school.common.R<java.util.List<com.wxy.school.entity.AddressBook>>
     */
    @GetMapping("/getAddressBookList/{id}")
    public R<List<AddressBook>> get(@PathVariable Long id){
        LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(AddressBook::getUserid, id);
        List<AddressBook> addressBookList = addressBookService.list(lambdaQueryWrapper);
        return R.success(addressBookList);
    }
    
    /**
     * @description: 更新用户修后的地址信息
     * @author: 王祥宇 
     * @param: addressBookDto 
     * @return: com.wxy.school.common.R<java.lang.String>
     */
    @PutMapping("/updateAddress")
    public R<String> update(@RequestBody AddressBookDto addressBookDto){
        BaseContext.setThreadLocal(addressBookDto.getUsername());
        // 判断更该后的地址信息是否为默认信息 如果不是则直接存储
        if (addressBookDto.getIsDefault() == 1){
            // 如果是默认地址 则查询用户是否已拥有一个默认地址 若没有则直接存储
            LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(AddressBook::getIsDefault, addressBookDto.getIsDefault());
            AddressBook addressBook1 = addressBookService.getOne(lambdaQueryWrapper);
            if (addressBook1 != null){
                // 若以拥有默认地址 则更该原地址信息为普通地址 并将用户提交的修改后的地址信息存储
                if (!addressBook1.getId().equals(addressBookDto.getId())){
                    addressBook1.setIsDefault(0);
                    addressBookService.updateById(addressBook1);
                }
            }
        }
        AddressBook addressBook = new AddressBook();
        BeanUtils.copyProperties(addressBookDto, addressBook, "username");
        addressBookService.updateById(addressBook);
        return R.success("地址修改成功");
    }
    
    /**
     * @description: 根据地址id删除地址信息
     * @author: 王祥宇 
     * @param: id 
     * @return: com.wxy.school.common.R<java.lang.String>
     */
    @DeleteMapping("/deleteAddress/{id}")
    public R<String> delete(@PathVariable Long id){
        addressBookService.removeById(id);
        return R.success("删除成功");
    }

    /**
     * @description: 根据地址id获取地址信息
     * @author: 王祥宇
     * @param: addressId
     * @return: com.wxy.school.common.R<com.wxy.school.entity.AddressBook>
     */
    @GetMapping("/getById/{addressId}")
    public R<AddressBook> getById(@PathVariable Long addressId){
        AddressBook addressBook = addressBookService.getById(addressId);
        return R.success(addressBook);
    }
}
